package com.example.mytayunapi.Mapper;

import com.example.mytayunapi.Entity.ordersInfo;
import com.example.mytayunapi.Vo.ordersuserproducts;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ordersMapper {
    @Insert("insert into orders(Product_id, order_name, School_degree, totalAmount) values(#{Product_id}, #{order_name}, #{School_degree}, #{totalAmount})")
    @Options(useGeneratedKeys = true, keyProperty = "order_id")
    int insertOrderAndGetKey(ordersInfo ordersinfo);

    @Select("SELECT * FROM orders WHERE order_id = #{orderId}")
    ordersInfo getOrderById(int orderId);

    @Select("SELECT \n" +
            "    u1.Nick_name AS buyer_name, \n" +
            "    u2.School_degree AS seller_school_degree,\n" +
            "    u2.Nick_name AS seller_name,\n" +
            "    p.*\n" +
            "FROM \n" +
            "    orders AS o\n" +
            "JOIN \n" +
            "    user AS u1 ON o.School_degree = u1.School_degree\n" +
            "JOIN \n" +
            "    products AS p ON o.Product_id = p.Product_id\n" +
            "JOIN \n" +
            "    user AS u2 ON p.School_degree = u2.School_degree where u1.School_degree = #{School_degree} and o.Order_status = '待支付'")
    List<ordersuserproducts> queryOrders(String School_degree);

    @Select("select * from orders where order_id = #{order_id}")
    ordersInfo SelectedByOrder_id(Integer order_id);


    @Update("update orders set Order_status = '已支付',alipayTradeNo = #{alipayTradeNo} where order_id = #{order_id}")
    Integer updateById(ordersInfo orders);

    @Select("select * from orders where Product_id = #{Product_id} and Order_status = '待支付'")
    List<ordersInfo> Selected_order_id(Integer Product_id);

    @Select("SELECT \n" +
            "    u1.Nick_name AS buyer_name, \n" +
            "    u2.School_degree AS seller_school_degree,\n" +
            "    u2.Nick_name AS seller_name,\n" +
            "    p.*,o.*\n" +
            "FROM \n" +
            "    orders AS o\n" +
            "JOIN \n" +
            "    user AS u1 ON o.School_degree = u1.School_degree\n" +
            "JOIN \n" +
            "    products AS p ON o.Product_id = p.Product_id\n" +
            "JOIN \n" +
            "    user AS u2 ON p.School_degree = u2.School_degree where u1.School_degree = #{School_degree} and o.Order_status = '已支付'")
    List<ordersuserproducts> SelectedByOrder_Status(String School_degree);

    @Select("select * from user u join orders o on u.School_degree = o.School_degree where order_id = #{order_id}")
    List<ordersuserproducts> getUser(Integer order_id);

    @Select("SELECT \n" +
            "    u1.Nick_name AS buyer_name, \n" +
            "    u2.School_degree AS seller_school_degree,\n" +
            "    u2.Nick_name AS seller_name,\n" +
            "    p.*,o.*\n" +
            "FROM \n" +
            "    orders AS o\n" +
            "JOIN \n" +
            "    user AS u1 ON o.School_degree = u1.School_degree\n" +
            "JOIN \n" +
            "    products AS p ON o.Product_id = p.Product_id\n" +
            "JOIN \n" +
            "    user AS u2 ON p.School_degree = u2.School_degree where order_id=#{order_id}")
    List<ordersuserproducts> queryOrdersByOrderId(Integer order_id);

    @Select("SELECT \n" +
            "    u1.Nick_name AS buyer_name, \n" +
            "    u2.School_degree AS seller_school_degree,\n" +
            "    u2.Nick_name AS seller_name,\n" +
            "    p.*,o.*\n" +
            "FROM \n" +
            "    orders AS o\n" +
            "JOIN \n" +
            "    user AS u1 ON o.School_degree = u1.School_degree\n" +
            "JOIN \n" +
            "    products AS p ON o.Product_id = p.Product_id\n" +
            "JOIN \n" +
            "    user AS u2 ON p.School_degree = u2.School_degree where order_id=#{order_id}")
    List<ordersuserproducts> queryOrdersRefundsByOrderId(Integer order_id);


}
